/*!
 * global functions 
 * Copyright(c) purenode
 * @author yangjian102621@gmail.com
 * MIT Licensed
 */

Date.prototype.format = function (format) {

	var map = {
		"M": this.getMonth() + 1, //月份 
		"d": this.getDate(), //日 
		"h": this.getHours(), //小时 
		"m": this.getMinutes(), //分 
		"s": this.getSeconds(), //秒 
		"q": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};

	var that = this;

	format = format.replace(/([yMdhmsqS])+/g, function(all, t){
		var v = map[t];
		if( v !== undefined ) {
			if( all.length > 1 ) {
				v = '0' + v;
				v = v.substr(v.length-2);
			}
			return v;

		} else if ( t === 'y' ) {
			return (that.getFullYear() + '').substr(4 - all.length);
		}
		return all;
	});
	return format;
}

//包含框架的类
global.F_require = function(file) {
	return require(__dirname+"/framework/"+file);
}

//简单对象合并继承
global.F_extends = function(org, merge) {

	for ( var name in merge ) {
		org[name] = merge[name];
	}
	return org;
}

//首字母大写
global.F_ucfirst = function(str) {
	var str = str.toLowerCase();
	str = str.replace(/\b\w+\b/g, function(word){
		return word.substring(0,1).toUpperCase()+word.substring(1);
	});
	return str;
}

/**
 * 清除字符串头部和尾部指定的字符串
 * @param str 待处理字符串
 * @param s 要清除的字符串，如果不传入此参数则默认清除空格
 */
global.F_trim = function(str, s) {
	if ( !s ) {
		return str.replace(/^\s+|\s+$/, '');
	} else {
		var regExp = new RegExp("^\\"+s+"+|\\"+s+"+$", "g");
		return str.replace(regExp, '');
	}
}

global.F_ltrim = function(str, s) {
	if ( !s ) {
		return str.replace(/^\s+/, '');
	} else {
		var regExp = new RegExp("^\\"+s+"+", "g");
		return str.replace(regExp, '');
	}

}

global.F_rtrim = function(str, s) {
	if ( !s ) {
		return str.replace(/\s+$/, '');
	} else {
		var regExp = new RegExp("\\"+s+"+$", "g");
		return str.replace(regExp, '');
	}
}

/** 以上是框架依赖的全局函数，请误删除 **/


global.F_url = function(url) {
	return "http://www.purenode.com/"+url;
}

/**
 * 输出格式化日期
 */
global.F_date = function(date, format) {
	return new Date(date).format(format);
}
